import React, { Component } from 'react'
import { Button, Text, TextInput, View } from 'react-native'
import {users} from './users.json'
/**
 * input: user.id
 * output: 
 *      onUserUpdate
 *          param: user
 */
export default class UserEditor extends Component {
    state={user:{}}
    componentDidMount(){
        let index=users.findIndex(user=>user.id===this.props.id)
        this.setState({user:users[index]})
    }
    _unameChange=uname=>{
        let user=this.state.user
        user.uname=uname
        this.setState({user})
    }
    _addressChange=address=>{
        let user=this.state.user
        user.address=address
        this.setState({user})
    }
    _confirm=()=>{
        this.props.onUserUpdate(this.state.user)
    }
    render() {
        return (
            <View>
                <TextInput placeholder="请输入姓名" value={this.state.user.uname} onChangeText={this._unameChange}/>
                <TextInput placeholder="请输入地址"  value={this.state.user.address} onChangeText={this._addressChange}/>
                <Button title="确定" onPress={this._confirm}/>
            </View>
        )
    }
}
